﻿using Gx.Common;
using Gx.Common.Core;
using Gx.Dal;
using Gx.Dal.Manage;
using Gx.Dal.Shared;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.Enums;
using Gx.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;


namespace Gx.Bll.Manage
{
    public class LKWarehouseOutBll
    {

        protected LKWarehouseOutDal dal = new LKWarehouseOutDal();
        protected BaseDal<LKWarehouseOutDetail> detailDal = new BaseDal<LKWarehouseOutDetail>();
        protected BaseDal<LKWarehouseOutCost> costDal = new BaseDal<LKWarehouseOutCost>();

        protected LKAttachmentBll attachmentBll = new LKAttachmentBll();

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public PagedList<LKWarehouseOut> GetLKWarehouseOutPageList(LKWarehouseOutInput input)
        {
            return dal.GetLKWarehouseOutPageList(input);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public List<LKWarehouseOut> GetLKWarehouseOuts()
        {
            return dal.GetAll().ToList();
        }

        /// <summary>
        /// 新增商品出库单
        /// </summary>
        /// <param name="lKWarehouseOutDto"></param>
        /// <returns></returns>
        public ReturnModel AddLKWarehouseOut(LKWarehouseOutDto lKWarehouseOutDto)
        {

            //var any = dal.AnyName(lKWarehouseOut.LevelName);
            //if (any)
            //{
            //    return new ReturnModel { Success = false, Msg = "名称已存在" };
            //}
            string maxCode = dal.GetMaxLKWarehouseOutCode();
            string code = "C" + DateTime.Now.ToString("yyMMdd") + "001";
            if (!string.IsNullOrWhiteSpace(maxCode))
            {
                var maxMonth = maxCode.Substring(2, 2);
                var nowMonth = DateTime.Now.ToString("MM");
                if (maxMonth == nowMonth)
                {
                    int num = Convert.ToInt32(maxCode);//Convert.ToInt32(maxCode.Substring(7, 3));
                    num++;
                    code = "C" + num.ToString();//DateTime.Now.ToString("yyMMdd") + num.ToString().PadLeft(3, '0');
                }
            }

            lKWarehouseOutDto.WarehouseOut.BillNo = code;

            lKWarehouseOutDto.WarehouseOut.CreateTime = DateTime.Now;
            lKWarehouseOutDto.WarehouseOut.CreateUserId = LoginHelper.GetUserId();
            lKWarehouseOutDto.WarehouseOut.CreateUser = LoginHelper.UserName();
            lKWarehouseOutDto.WarehouseOut.CompanyId = UserContext.CompanyId;
            var result = dal.AddLKWarehouseOut(lKWarehouseOutDto);//dal.Insert(lKWarehouseOutDto.WarehouseOut);

            if (result && lKWarehouseOutDto.AttachmentId > 0)
            {
                attachmentBll.SaveLKAttachment(new LKAttachment
                {
                    Id = lKWarehouseOutDto.AttachmentId,
                    BillNo = code,
                    BillType = (int)BillTypeTemplate.WarehouseOut,
                });
            }
            return new ReturnModel { Success = result, Msg = result ? "新增成功" : "新增失败" };
        }

        /// <summary>
        /// 编辑商品出库单
        /// </summary>
        /// <param name="lKWarehouseOut"></param>
        /// <returns></returns>
        public bool EditLKWarehouseOut(LKWarehouseOutDto lKWarehouseOutDto)
        {
            var lKWarehouseOut= lKWarehouseOutDto.WarehouseOut;
            var wout = dal.Get(lKWarehouseOut.Id);
            if (wout == null)
            {
                return false;
            }
            if (wout.CompanyId != UserContext.CompanyId) return false;
            lKWarehouseOut.BillNo = wout.BillNo;
            lKWarehouseOut.CreateTime = wout.CreateTime;
            lKWarehouseOut.CreateUserId = wout.CreateUserId;
            lKWarehouseOut.CreateUser = wout.CreateUser;
            lKWarehouseOut.ModifyUserId = LoginHelper.GetUserId();
            lKWarehouseOut.ModifyUser = LoginHelper.UserName();
            lKWarehouseOut.ModifyTime = DateTime.Now;
            lKWarehouseOut.CompanyId = UserContext.CompanyId;
            var result= dal.EditLKWarehouseOut(lKWarehouseOutDto);//dal.Update(lKWarehouseOut);

            if (result && lKWarehouseOutDto.AttachmentId > 0)
            {
                attachmentBll.DeleteLKAttachment(lKWarehouseOutDto.WarehouseOut.BillNo);
                attachmentBll.SaveLKAttachment(new LKAttachment
                {
                    Id = lKWarehouseOutDto.AttachmentId,
                    BillNo = lKWarehouseOutDto.WarehouseOut.BillNo,
                    BillType = (int)BillTypeTemplate.WarehouseOut,
                });
            }

            return result;
        }

        /// <summary>
        /// 删除商品出库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnModel DeleteLKWarehouseOut(int id)
        {
            var bill = dal.Get(id);
            if (bill == null) return new ReturnModel { Success = false, Msg = "出库单不存在" };
            if(bill.IsReview)
                return new ReturnModel { Success = false, Msg = "已复核的出库单不能删除" };
            var result= dal.DeleteLKWarehouseOut(id);
            if (result)
            {
                attachmentBll.DeleteLKAttachment(bill.BillNo);
            }
            return new ReturnModel { Success = result, Msg = result ? "删除成功" : "删除失败" };
            
        }

        /// <summary>
        /// 获取商品出库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LKWarehouseOutDto GetLKWarehouseOutById(int id)
        {
            var bill = dal.Get(id);

            if (bill.CompanyId != UserContext.CompanyId)
                return new LKWarehouseOutDto();

            var detailList = detailDal.GetListByWhere("OutId=@id", new { id }).ToList();
            var costList = costDal.GetListByWhere("OutId=@id", new { id }).ToList();

            var attachment = attachmentBll.GetLKAttachmentView(bill.BillNo);
            return new LKWarehouseOutDto { WarehouseOut = bill, Details = detailList, Costs = costList, FilePath = attachment?.AttachmentName, FileName = attachment?.OriginalName };
        }

        public List<LKWarehouseOutDetail> GetLKWarehouseOutDetails(int id)
        {
            return dal.GetKWarehouseOutDetails(id);
        }

        /// <summary>
        /// 复核出库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ReviewLKWarehouseOut(int id)
        {
            var bill = dal.Get(id);
            if (bill == null) return false;
            if (bill.CompanyId != UserContext.CompanyId) return false;
            bill.IsReview = true;
            bill.ReviewerId = LoginHelper.GetUserId();
            bill.Reviewer = LoginHelper.UserName();
            bill.ReviewTime = DateTime.Now;

            return dal.Update(bill);
        }
    }
}

